package com.xmy.study.listeners;

import com.xmy.study.constants.MqConstants;
import com.xmy.study.entity.ChaPlan;
import com.xmy.study.respons.ChaStudyPlan;
import com.xmy.study.respons.ResStudentAccount;
import com.xmy.study.respons.StudentAllotTier;
import com.xmy.study.service.IChaPlanService;
import com.xmy.study.service.IMqPushService;
import com.xmy.study.service.IStudentAccountService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.amqp.core.ExchangeTypes;
import org.springframework.amqp.rabbit.annotation.Exchange;
import org.springframework.amqp.rabbit.annotation.Queue;
import org.springframework.amqp.rabbit.annotation.QueueBinding;
import org.springframework.amqp.rabbit.annotation.RabbitListener;
import org.springframework.amqp.rabbit.core.RabbitTemplate;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Component;

import java.math.BigDecimal;

@Slf4j
@Component
public class StudentAccountListener {


    @Autowired
    IStudentAccountService iStudentAccountService;

    @Autowired
    IChaPlanService iChaPlanService;

    @Autowired
    IMqPushService iMqPushService;

    @Value("${subjectsId}")
    String SUBJECTS_IDS;


    @RabbitListener(bindings = @QueueBinding(
            value = @Queue(name = MqConstants.QUEUE_SYNC_STUDENT_ACCOUNT_MATH_Q1, durable = "true"),
            exchange = @Exchange(name = MqConstants.EXCHANGE_SYNC_STUDENT_ACCOUNT_FANOUT, type = ExchangeTypes.FANOUT)
    ))
    public void listenDirectQueueAddAccount(ResStudentAccount resStudentAccount) throws InterruptedException {
        //System.out.println("消费者math 收到了 direct.queue1的消息：【" + resStudentAccount +"】");
        if (resStudentAccount.getSubjectsId() != null && (resStudentAccount.getSubjectsId().equals(Long.parseLong(SUBJECTS_IDS)))) {
            iStudentAccountService.updateAccount(resStudentAccount);
        }
    }


    /*@RabbitListener(bindings = @QueueBinding(
            value = @Queue(name = MqConstants.QUEUE_MATH_ADD_STUDENT_ACCOUNT_Q1, durable = "true"),
            exchange = @Exchange(name = MqConstants.EXCHANGE_MATH_ADD_STUDENT_ACCOUNT_DIRECT, type = ExchangeTypes.DIRECT),
            key = MqConstants.KEY_MATH
    ))
    public void listenDirectQueueAddAccount(ResStudentAccount resStudentAccount) throws InterruptedException {
        System.out.println("消费者english 收到了 direct.queue1的消息：【" + resStudentAccount +"】");
        if (resStudentAccount.getSubjectsId().equals(2L)){
            iStudentAccountService.updateAccount(resStudentAccount);
        }

    }*/

    /**
     * 判断是否通关，已通关不算应到
     */
    @RabbitListener(bindings = @QueueBinding(
            value = @Queue(name = MqConstants.QUEUE_CHA_STUDY_STATUS_MATH_Q1, durable = "true"),
            exchange = @Exchange(name = MqConstants.EXCHANGE_CHA_STUDY_STATUS, type = ExchangeTypes.FANOUT)
    ))
    public void chaStudyPlanAdd(ChaStudyPlan chaStudyPlan) {
        //System.err.println("消费者收到了 direct.queue1的消息：【" + chaStudyPlan + "】");
        if (SUBJECTS_IDS.equals(chaStudyPlan.getSubjectsId().toString())) {
            // 判断是否已通关
            if (chaStudyPlan.getRightNum() != null && chaStudyPlan.getAmendNum() != null && chaStudyPlan.getLowestShutnum() != null) {
//                if ((chaStudyPlan.getRightNum().intValue()) * 100 / chaStudyPlan.getLowestShutnum().intValue() >= chaStudyPlan.getPassRade()) {
                if (chaStudyPlan.getRightNum() + chaStudyPlan.getAmendNum() >= chaStudyPlan.getLowestShutnum()) {
                    iMqPushService.chaStudyPlanAdd(chaStudyPlan);
                }
            }
        }

    }


    /**
     * 数学正确率分层设置同步到财务
     */
    @RabbitListener(bindings = @QueueBinding(
            value = @Queue(name = MqConstants.QUEUE_CHA_STUDY_ACCURACY_MATH_Q1, durable = "true"),
            exchange = @Exchange(name = MqConstants.EXCHANGE_CHA_STUDY_ACCURACY_FANOUT, type = ExchangeTypes.FANOUT)
    ))
    public void chaStudyPlanAccuracy(ChaStudyPlan chaStudyPlan) {
        //System.err.println("消费者收到了 direct.queue1的消息：【" + chaStudyPlan + "】");
        try {
            if (SUBJECTS_IDS.equals(chaStudyPlan.getSubjectsId().toString())) {
                // 获取该学生本学期的正确率
                ChaPlan chaPlan = iChaPlanService.getPassRadeDetail(String.valueOf(chaStudyPlan.getYearPart()), String.valueOf(chaStudyPlan.getQuarterNum()), null, "2", "1", null, null, null, null, String.valueOf(chaStudyPlan.getSubjectsId()), "", "", "2", "2", "2", 1, chaStudyPlan.getStudentId());



                ChaStudyPlan studyPlan = new ChaStudyPlan();
                studyPlan.setYearPart(chaStudyPlan.getYearPart());
                studyPlan.setQuarterNum(chaStudyPlan.getQuarterNum());
                studyPlan.setStudentId(chaStudyPlan.getStudentId());
                studyPlan.setSubjectsId(chaStudyPlan.getSubjectsId());

                StudentAllotTier studentAllotTier = new StudentAllotTier();
                studentAllotTier.setStudentId(chaStudyPlan.getStudentId());
                studentAllotTier.setYearPart(chaStudyPlan.getYearPart());
                studentAllotTier.setQuarterNum(chaStudyPlan.getQuarterNum());
                studentAllotTier.setSubjectsId(chaStudyPlan.getSubjectsId());
                BigDecimal rightRade = BigDecimal.ZERO;
                if (chaPlan!=null){
                    studyPlan.setLowestShutnumAll(chaPlan.getLowestShutnumAll());
                    studyPlan.setRightNumAll(chaPlan.getRightNumAll());
                    BigDecimal rightNum = new BigDecimal(studyPlan.getRightNumAll());
                    BigDecimal lowestShutNum = new BigDecimal(studyPlan.getLowestShutnumAll());
                    if (lowestShutNum.compareTo(BigDecimal.ZERO)>0) {
                        rightRade = rightNum.divide(lowestShutNum,6,BigDecimal.ROUND_HALF_UP);
                        rightRade = rightRade.multiply(new BigDecimal(100)).setScale(2, BigDecimal.ROUND_HALF_UP);
                    }
                }
                //studentAllotTier.setYearClassId(chaStudyPlan.get());
                studentAllotTier.setRightRade(rightRade);
                studentAllotTier.setAddType(1);
                iMqPushService.studentAllotTier(studentAllotTier);
            }
        }catch (Exception e){
            System.out.println(e.getMessage());
        }
    }

}
